package com.wenwei.paper.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wenwei.paper.R;
import com.wenwei.paper.adapter.UserMenuAdapter;
import com.wenwei.paper.base.BaseActivity;
import com.wenwei.paper.base.Constant;
import com.wenwei.paper.bean.PageBean;
import com.wenwei.paper.bean.ReplenishmentEndListBean;
import com.wenwei.paper.net.ApiManager;
import com.wenwei.paper.net.BaseSubscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/13.
 */
public class UserMenuActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.recycler)
    XRecyclerView recycler;

    UserMenuAdapter mAdapter;
    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_user_menu;
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("我的清单");
        recycler.setLayoutManager(new LinearLayoutManager(mContext));
        recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                recycler.setLoadingMoreEnabled(true);
                getReplenishmentEnd(1);
            }

            @Override
            public void onLoadMore() {
                if (currentPage < pageTotalCount) {
                    currentPage = currentPage + 1;
                    recycler.setLoadingMoreEnabled(true);
                    getReplenishmentEnd(2);
                } else {
                    recycler.setLoadingMoreEnabled(false);
                    recycler.loadMoreComplete();
                }
            }
        });
        mAdapter = new UserMenuAdapter();
        recycler.setAdapter(mAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.base_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void initData() {
        super.initData();
        getReplenishmentEnd(1);
    }

    private int currentPage = 1;//当前分页
    private int pageTotalCount;//分页总数
    ArrayList<ReplenishmentEndListBean> mList = new ArrayList<>();
    private BaseSubscriber<PageBean<ArrayList<ReplenishmentEndListBean>>> replenishmentSubscriber;
    private void getReplenishmentEnd(final int type){
        replenishmentSubscriber = new BaseSubscriber<PageBean<ArrayList<ReplenishmentEndListBean>>>(mContext) {
            @Override
            public void onSuccess(PageBean<ArrayList<ReplenishmentEndListBean>> replenishmentEndListBeen) {
                pageTotalCount = replenishmentEndListBeen.getLast_page();

                if(type == Constant.LOAD_TYPE_REFRESH){
                    mList.clear();
                    mList.addAll(replenishmentEndListBeen.getData());
                    mAdapter.setMenuData(mList);
                    recycler.refreshComplete();
                }else{
                    mList.addAll(replenishmentEndListBeen.getData());
                    mAdapter.setMenuData(mList);
                    recycler.loadMoreComplete();
                }
                mAdapter.notifyDataSetChanged();
            }
        };
        ApiManager.getInstance().getReplenishmentEnd(replenishmentSubscriber,currentPage);
    }
}
